package com.appspot.mypartymanager.service.impl;

import java.util.List;

import com.appspot.mypartymanager.dao.PartyDAO;
import com.appspot.mypartymanager.dao.impl.PartyDAOImpl;
import com.appspot.mypartymanager.entity.party.Party;
import com.appspot.mypartymanager.exception.InvalidPartyException;
import com.appspot.mypartymanager.service.PartyService;

public class PartyServiceImpl implements PartyService {
	
	private static PartyDAO DAO = PartyDAOImpl.getInstance();

	@Override
	public void createParty(Party newParty) throws InvalidPartyException {

		if (!isValid(newParty)) {
			throw new InvalidPartyException();
		}

		DAO.createParty(newParty);
	}

	@Override
	public Party getPartyById(Long id) {
		
		return DAO.getPartyById(id);
		
	}

	@Override
	public List<Party> getPartysByUser(String ownerUserGoogleAcount) {

		// validation
		if (ownerUserGoogleAcount == null
				|| ownerUserGoogleAcount.trim().equals("")) {
			throw new IllegalArgumentException();
		}

		return DAO.getPartysByUser(ownerUserGoogleAcount);

	}

	@Override
	public void deleteParty(Long id) {

		DAO.deleteParty(id);

	}

	@Override
	public void updateParty(Party partyToUpdate) throws InvalidPartyException {

		if (!isValid(partyToUpdate)) {
			throw new InvalidPartyException();
		}

		DAO.updateParty(partyToUpdate);
	}

	/**
	 * Validation rule to check if the Party object has
	 * mandatory attributes
	 * @param Party
	 */
	public boolean isValid(Party e) {

		if (e.getDateCreated() == null || e.getOwnerGoogleAcount() == null
				|| e.getOwnerGoogleAcount().trim().equals("")
				|| e.getName() == null || e.getName().trim().equals("")) {

			return false;
		}

		return true;

	}

}
